package org.jeecg.modules.crawler.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;


import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.crawler.entity.CueCrawlerInfo;
import org.jeecg.modules.crawler.service.ICueCrawlerInfoService;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 爬虫信息
 * @Author: jeecg-boot
 * @Date: 2020-05-02
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "爬虫信息")
@RestController
@RequestMapping("/crawler/cueCrawlerInfo")
public class CueCrawlerInfoController extends JeecgController<CueCrawlerInfo, ICueCrawlerInfoService> {
    @Autowired
    private ICueCrawlerInfoService cueCrawlerInfoService;

    /**
     * 分页列表查询
     *
     * @param cueCrawlerInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "爬虫信息-分页列表查询")
    @ApiOperation(value = "爬虫信息-分页列表查询", notes = "爬虫信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CueCrawlerInfo cueCrawlerInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CueCrawlerInfo> queryWrapper = QueryGenerator.initQueryWrapper(cueCrawlerInfo, req.getParameterMap());
        Page<CueCrawlerInfo> page = new Page<CueCrawlerInfo>(pageNo, pageSize);
        queryWrapper.orderByDesc("create_time");
        IPage<CueCrawlerInfo> pageList = cueCrawlerInfoService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param cueCrawlerInfo
     * @return
     */
    @AutoLog(value = "爬虫信息-添加")
    @ApiOperation(value = "爬虫信息-添加", notes = "爬虫信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CueCrawlerInfo cueCrawlerInfo) {
        cueCrawlerInfoService.save(cueCrawlerInfo);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param cueCrawlerInfo
     * @return
     */
    @AutoLog(value = "爬虫信息-编辑")
    @ApiOperation(value = "爬虫信息-编辑", notes = "爬虫信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CueCrawlerInfo cueCrawlerInfo) {
        cueCrawlerInfoService.updateById(cueCrawlerInfo);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "爬虫信息-通过id删除")
    @ApiOperation(value = "爬虫信息-通过id删除", notes = "爬虫信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        cueCrawlerInfoService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "爬虫信息-批量删除")
    @ApiOperation(value = "爬虫信息-批量删除", notes = "爬虫信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.cueCrawlerInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "爬虫信息-通过id查询")
    @ApiOperation(value = "爬虫信息-通过id查询", notes = "爬虫信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CueCrawlerInfo cueCrawlerInfo = cueCrawlerInfoService.getById(id);
        return Result.ok(cueCrawlerInfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param cueCrawlerInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CueCrawlerInfo cueCrawlerInfo) {
        return super.exportXls(request, cueCrawlerInfo, CueCrawlerInfo.class, "爬虫信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CueCrawlerInfo.class);
    }


    @AutoLog(value = "爬虫信息-初始化关键词和规则")
    @ApiOperation(value = "爬虫信息-初始化关键词和规则", notes = "爬虫信息-初始化关键词和规则")
    @GetMapping(value = "/initCrawlerData")
    public Result<?> initCrawlerData() {
        Map<String, Object> retMap = cueCrawlerInfoService.initCrawlerData();
        return Result.ok(retMap);
    }

    @AutoLog(value = "爬虫信息-设置关键词")
    @ApiOperation(value = "爬虫信息-设置关键词", notes = "爬虫信息-设置关键词")
    @GetMapping(value = "/keywordOk")
    public Result<?> keywordOk(@RequestParam(name = "keyword", required = true) String keyword) {
        cueCrawlerInfoService.keywordOk(keyword);
        return Result.ok();
    }

    @AutoLog(value = "爬虫信息-设置规则")
    @ApiOperation(value = "爬虫信息-设置规则", notes = "爬虫信息-设置规则")
    @GetMapping(value = "/ruleOk")
    public Result<?> ruleOk(@RequestParam Map<String, Object> keyword) {
        cueCrawlerInfoService.ruleOk(keyword);
        return Result.ok();
    }


    @AutoLog(value = "爬虫信息-改变爬虫状态")
    @ApiOperation(value = "爬虫信息-改变爬虫状态", notes = "爬虫信息-改变爬虫状态")
    @GetMapping(value = "/changeCrawlerState")
    public Result<?> changeCrawlerState(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "executeState", required = true) String executeState) {
        cueCrawlerInfoService.changeCrawlerState(id, executeState.equals("0") ? "1" : "0");
        return Result.ok();
    }

//
//    @AutoLog(value = "爬虫信息-手动执行爬虫")
//    @ApiOperation(value = "爬虫信息-手动执行爬虫", notes = "爬虫信息-改变爬虫状态")
//    @GetMapping(value = "/excuteCrawler")
//    public Result<?> excuteCrawler(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "executeState", required = true) String executeState) {
//        if (executeState.equals("0")) {
//            SpiderThread sr = new SpiderThread(id);
//            sr.start();
//        }
//        return Result.ok();
//    }


	@AutoLog(value = "爬虫信息-查询爬虫执行记录")
	@ApiOperation(value = "爬虫信息-查询爬虫执行记录", notes = "爬虫信息-查询爬虫执行记录")
	@GetMapping(value = "/querySpiderLog")
	public Result<?> querySpiderLog(@RequestParam(name = "id", required = true) String id) {
    	List<Map<String,Object>> retMap = cueCrawlerInfoService.querySpiderLog(id);
		return Result.ok(retMap);
	}




}
